import facade
from xjlibrary.our_file_dir import BaseDir

curpath = BaseDir.get_file_dir_absolute(__file__)
TopPath = BaseDir.get_upper_dir(curpath, -2)
dirPath = BaseDir.get_new_path(TopPath, "download", "yuetongthesis", "download", "thesishtml")
BaseDir.create_dir(dirPath)
configfile = BaseDir.get_new_path(curpath, "db.ini")


class Downthesis(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Host": "zt.yuetongbook.cn",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
        }

    def select(self):
        sql = "select rawid,url from thesis where stat=0"
        rows = self.mysqlutils.SelectFromDB(sql)
        for row in rows:
            if BaseDir.is_file_exists(BaseDir.get_new_path(dirPath, row[0] + ".html")):
                self.logger.info("文件存在")
                sql = "update thesis set stat=1 where rawid='{}'".format(row[0])
                self.mysqlutils.ExeSqlToDB(sql)
                continue

            if self.down_book(row):
                sql = "update thesis set stat=1 where rawid='{}'".format(row[0])
                self.mysqlutils.ExeSqlToDB(sql)

    def down_book(self, row):
        url = "http://zt.yuetong.cn" + row[1]
        BoolResult, errString, r = facade.BaseRequest(url,
                                                      headers=self.header,
                                                      mark=row[0],
                                                      timeout=(30, 60))
        if BoolResult:
            BaseDir.single_write_file(r.text, BaseDir.get_new_path(dirPath, row[0] + ".html"))
            return True
        else:
            return False


if __name__ == "__main__":
    down = Downthesis()
    down.select()
